home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE2 / PD / VINCE / !ViNCe / c / hextile8 < prev    next >
Text File  |  2002-03-10  |  6KB  |  184 lines

  1. /*
  2.  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
  3.  *
  4.  *  This is free software; you can redistribute it and/or modify
  5.  *  it under the terms of the GNU General Public License as published by
  6.  *  the Free Software Foundation; either version 2 of the License, or
  7.  *  (at your option) any later version.
  8.  *
  9.  *  This software is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  *
  14.  *  You should have received a copy of the GNU General Public License
  15.  *  along with this software; if not, write to the Free Software
  16.  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  17.  *  USA.
  18.  */
  19.  
  20. /*
  21.  * hextile.c - handle hextile encoding.
  22.  *
  23.  * This file shouldn't be compiled directly.  It is included multiple times by
  24.  * rfbproto.c, each time with a different definition of the macro BPP.  For
  25.  * each value of BPP, this file defines a function which handles a hextile
  26.  * encoded rectangle with BPP bits per pixel.
  27.  */
  28.  
  29.  
  30. /*
  31.  * alternative hextile coding of 16x16 pixels rectangle
  32.  * flag byte followed by optional data
  33.  * flag byte:
  34.  *        bit 0 set           bg colour included
  35.  *        bit 1..3            coding type
  36.  *                            0         hextile
  37.  *                            1         raw
  38.  *        bit 4 set           1x1 data included (hextile coding)
  39.  *        bit 5 set           1x2 data included (hextile coding)
  40.  *        bit 6 set           2x1 data included (hextile coding)
  41.  *        bit 7 set           any size rectangle data included (hextile coding)
  42.  *        bit 4..6            no. of bits per pixel (raw coding)
  43.  *                            0         native
  44.  *                            1         1bpp with palette
  45.  *                            2         2bpp with palette
  46.  *                            3         4bpp with palette
  47.  *                            4         6bpp RGB222
  48.  *                            5         8bpp RGB332
  49.  *
  50.  * if bit 0 set, first data byte(s) holds new bgcolour
  51.  * if hextile coding is used, 1x1 data comes first (if any), then 1x2 etc.
  52.  *
  53.  * 1x1 data:
  54.  *    byte 0  1x1 status byte
  55.  *        bit 0 set           first data byte(s) holds new fg colour
  56.  *        bit 1 set           there's another 1x1 status byte after the data
  57.  *        bit 2..7            no. of subrectangles - 1
  58.  *    followed by 4 bit X and 4 bit Y coordinates for each 1x1 rectangle
  59.  * 1x2 data:
  60.  *    byte 0  1x2 status byte
  61.  *        bit 0 set           first data byte(s) holds new fg colour
  62.  *        bit 1 set           there's another 1x2 status byte after the data
  63.  *        bit 2..7            no. of sub rectangles - 1
  64.  *    followed by 4 bit X and 4 bit Y coordinates for each 1x2 rectangle
  65.  * 2x1 data:
  66.  *    byte 0  2x1 status byte
  67.  *        bit 0 set           first data byte(s) holds new fg colour
  68.  *        bit 1 set           there's another 2x1 status byte after the data
  69.  *        bit 2..7            no. of sub rectangles - 1
  70.  *    followed by 4 bit X and 4 bit Y coordinates for each 2x1 rectangle
  71.  * any size rectangle data:
  72.  *    byte 0  status byte
  73.  *        bit 0 set           first data byte(s) holds new fg colour
  74.  *        bit 1 set           there's another status byte after the data
  75.  *        bit 2..7            no. of sub rectangles - 1
  76.  *    followed by 4 bit X and 4 bit Y coordinates and 4 bit
  77.  *    width and 4 bit height for each rectangle
  78.  * raw coding:
  79.  *    followed by palette (if bpp = 1, 2 or 4)
  80.  *    followed by raw data
  81.  *
  82.  * How to:
  83.  *  1) modify testColours() to count the no. of colours (bad idea for > 8bpp):
  84.  *        if (!(colourmask[*data>>3] & (1<<(*data &7))))
  85.  *          ncolours++;
  86.  *          colourmask[*data>>3] |= (1<<(*data &7))
  87.  *  2)  find subrectangles as usual but store differently
  88.  *        if (thew==1 && theh==1)
  89.  *           *subrects_1x1++ = rfbHextilePackXY(thex, they)
  90.  *           *subrects_1x1++ = colour
  91.  *           nsubrects_1x1++;
  92.  *           estimated_size++;
  93.  *           if (colour != prevcolour)  prevcolour=colour, estimated_size++;
  94.  *        likewise for 1x2, 2x1 and any size
  95.  *  3) when outputting hextile coded data:
  96.  *     output all 1x1 then all 1x2 etc.
  97.  */
  98.  
  99. static Bool HandleHextile8 (int rx, int ry, int rw, int rh)
  100. {
  101.   CARD8 bg, fg;
  102.   int i;
  103.   CARD8 *ptr;
  104.   int x, y, w, h;
  105.   int sx, sy, sw, sh;
  106.   CARD8 subencoding;
  107.   CARD8 nSubrects;
  108.  
  109.   for (y = ry; y < ry+rh; y += 16) {
  110.     for (x = rx; x < rx+rw; x += 16) {
  111.       w = h = 16;
  112.       if (rx+rw - x < 16)
  113.     w = rx+rw - x;
  114.       if (ry+rh - y < 16)
  115.     h = ry+rh - y;
  116.  
  117.       if (!ReadFromRFBServer((char *)&subencoding, 1))
  118.     return False;
  119.  
  120.       if (subencoding & rfbHextileRaw) {
  121.     if (!ReadFromRFBServer(buffer, w * h * (BPP / 8)))
  122.       return False;
  123.  
  124.         display_raw(x, y, w, h, buffer, 0);
  125.     continue;
  126.       }
  127.  
  128.       if (subencoding & rfbHextileBackgroundSpecified)
  129.     if (!ReadFromRFBServer((char *)&bg, sizeof(bg)))
  130.       return False;
  131.  
  132.       display_fillrectangle(x, y, w, h, rgb332_rgb555_table[bg]);
  133.  
  134.       if (subencoding & rfbHextileForegroundSpecified)
  135.     if (!ReadFromRFBServer((char *)&fg, sizeof(fg)))
  136.       return False;
  137.  
  138.       if (!(subencoding & rfbHextileAnySubrects)) {
  139.     continue;
  140.       }
  141.  
  142.       if (!ReadFromRFBServer((char *)&nSubrects, 1))
  143.     return False;
  144.  
  145.       ptr = (CARD8 *)buffer;
  146.  
  147.       if (subencoding & rfbHextileSubrectsColoured) {
  148.     if (!ReadFromRFBServer(buffer, nSubrects * (2 + (BPP / 8))))
  149.       return False;
  150.         subrect += nSubrects;
  151.  
  152.     for (i = 0; i < nSubrects; i++) {
  153.       fg = ptr[0];
  154.       ptr += 1;
  155.       sx = rfbHextileExtractX(*ptr);
  156.       sy = rfbHextileExtractY(*ptr);
  157.       ptr++;
  158.       sw = rfbHextileExtractW(*ptr);
  159.       sh = rfbHextileExtractH(*ptr);
  160.       ptr++;
  161.           display_fillrectangle(x+sx, y+sy, sw, sh, rgb332_rgb555_table[fg]);
  162.     }
  163.  
  164.       } else {
  165.     if (!ReadFromRFBServer(buffer, nSubrects * 2))
  166.       return False;
  167.         subrect += nSubrects;
  168.  
  169.     for (i = 0; i < nSubrects; i++) {
  170.       sx = rfbHextileExtractX(*ptr);
  171.       sy = rfbHextileExtractY(*ptr);
  172.       ptr++;
  173.       sw = rfbHextileExtractW(*ptr);
  174.       sh = rfbHextileExtractH(*ptr);
  175.       ptr++;
  176.           display_fillrectangle(x+sx, y+sy, sw, sh, rgb332_rgb555_table[fg]);
  177.     }
  178.       }
  179.     }
  180.   }
  181.  
  182.   return True;
  183. }
  184.